#==============================================================================
# Theta Scripting Engine - Equalize by privateer
#==============================================================================
# ► Version  : 1.0
#==============================================================================
# ► Introduction
# This script enables you to create weapons, skills, and items that equalizes
# its user's HP with its target's HP. Meaning, it will get the total of both
# battler's HP, divide it by 2, and set both of their HP to it.
#==============================================================================
# ► Instruction
# Just put this script anywhere below Scene_Debug but above Main.
#==============================================================================
# ► Compatibility
# Designed and tested for DBS. May or not work with exotic CBS and/or scripts
# that alter how actions are done in battle.
#==============================================================================

module TSE
  module Equalize
    # Weapons, Skills, and Items that have this Element ID will equalize
    # their user's HP with their target's HP.
    EQUALIZE_ELEMENT_ID = 17
  end
end

#==============================================================================
# DON'T EDIT ANY FURTHER UNLESS YOU KNOW WHAT YOU'RE DOING!
#==============================================================================

$imported = {} if $imported.nil?
$imported["TSE-Equalize"] = true

module RPG
  class Skill
    def equalize_skill?
      self.element_set.include?(TSE::Equalize::EQUALIZE_ELEMENT_ID)
    end
  end
  class Weapon
    def equalize_weapon?
      self.element_set.include?(TSE::Equalize::EQUALIZE_ELEMENT_ID)
    end
  end  
  class Item
    def equalize_item?
      self.element_set.include?(TSE::Equalize::EQUALIZE_ELEMENT_ID)
    end
  end    
end

class Game_Battler
  alias tse_equalize_attack_effect attack_effect
  def attack_effect(attacker)
    tse_equalize_attacker_effect(user, skill)    
    if attacker.is_?(Game_Actor) && $data_weapons[attacker.weapon_id].equalize_weapon?
      equalized_hp = (user.hp + self.hp) / 2.round
      self.damage = -(equalized_hp - self.hp)
      user.damage = -(equalized_hp - user.hp)
      self.hp = equalized_hp
      user.hp = equalized_hp
    end
  end  
  alias tse_equalize_skill_effect skill_effect
  def skill_effect(user, skill)
    tse_equalize_skill_effect(user, skill)    
    if skill.equalize_skill? && user != self
      equalized_hp = (user.hp + self.hp) / 2.round
      self.damage = -(equalized_hp - self.hp)
      user.damage = -(equalized_hp - user.hp)
      self.hp = equalized_hp
      user.hp = equalized_hp
    end
  end
  alias tse_equalize_item_effect item_effect
  def item_effect(user, item)
    tse_equalize_item_effect(user, item)    
    if item.equalize_item? && user != self
      equalized_hp = (user.hp + self.hp) / 2.round
      self.damage = -(equalized_hp - self.hp)
      user.damage = -(equalized_hp - user.hp)
      self.hp = equalized_hp
      user.hp = equalized_hp
    end
  end  
end
